//logs.js
var util = require('../../utils/util.js')
var router = require('../../utils/router.js')
var approuter = router.router
var app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    classItems: [],
    sessionKey:'',
    indicatorDots: true,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    userLogin: '',
    allClassUrl: '',
    orderClassUrl:'',
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },
  // publish课程
  create:function(){
    wx.navigateTo({
      url: '../../pages/publish/publish'
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: ''
    })
  },
  
  onLoad: function(query) {
    var that = this
    
    console.log("++++++++++++++++++++++++++++++++++++++")
    that.setData({
      userLogin: approuter.userLogin,
      allClassUrl: approuter.allClassUrl,
      orderClassUrl: approuter.orderClassUrl,
    })
    // 是否是从其他页面跳转过来
    // 不是则加载本地缓存
    // 是就重新拉取
    // url,method,data,header
    var sessionKey = wx.getStorageSync('LoginSessionKey');
    if (sessionKey){
      that.setData({
        sessionKey:sessionKey
      })
      // 有sessionkey 获取课程
      var header = { 'content-type': 'application/json', 'Cookie': 'JSESSIONID=' + sessionKey }
      var data = "sessionKey:"+sessionKey
      util.sendRequest(that.data.allClassUrl,'get',data,header)
        .then(function (reponse){
          // 成功调用
          // 成功调用
          if (reponse.data.Status == 'SUCCESS') {
            that.setData({
              // 设置值
              classItems:reponse.data.Data
            })
          }
          console.log(reponse)
        },function (error) {
          // 失败调用
          console.log(error)
        })
    }else{
      wx.login({
        success:function(res){
          var header = { 'content-type': 'application/x-www-form-urlencoded' }
          wx.request({
            url: that.data.userLogin,
            data: { code: res.code },
            method: 'GET',
            header: header,
            success: res => {
              console.log(res)
              if (res.data.Status == 'SUCCESS' && res.data.result.Status == 'SUCCESS') {
                wx.setStorageSync('LoginSessionKey', res.data.result.SessionKey)
                that.setData({
                  sessionKey: res.data.result.SessionKey
                })
              }
            }
          })
        }
      })
    }

     //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      // 更新数据
      console.log(userInfo)
      that.setData({
        userInfo:userInfo
      })
    })
    if (query['publisherId'] != undefined && query['classId'] != undefined) {
      // 查询填充
      
    }
  },
  // 监听onshow事件
  onShow:function(){
    var that = this
    console.log(app.globalData.switchtomy)
    if (app.globalData.switchtomy){
      var sessionKey = wx.getStorageSync('LoginSessionKey');
      if (sessionKey) {
        var header = { 'content-type': 'application/json', 'Cookie': 'JSESSIONID=' + sessionKey }
        var data = "sessionKey:" + sessionKey
        util.sendRequest(this.data.allClassUrl, 'get', data, header)
          .then(function (reponse) {
            // 成功调用
            // 成功调用
            if (reponse.data.Status == 'SUCCESS') {
              that.setData({
                // 设置值
                classItems: reponse.data.Data
              })
            }
            console.log(reponse)
          }, function (error) {
            // 失败调用
            console.log(error)
          })
      }
    }
  },


  navtopreorder:function(){
    wx.navigateTo({
      url: '../../pages/preorder/preorder',
    })
  },
  navtoclass:function(){
    wx.navigateTo({
      url: '../../pages/class/class',
    })
  },
  // 转发朋友
  onShareAppMessage: function (res) {
    var publisherid
    var classid
    if (res.from === 'button') {
      console.log(res.target)
      publisherid = res.target.dataset.publisherid
      classid = res.target.dataset.classid
      console.log(publisherid + " "+ classid)
    }
    return {
      title: '预约课',
      path: '/pages/my/my?publisherId='+publisherid+',classId='+classid,
      success: function(res) {
        console.log(res)
        // 转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },

  // 预约课程
  orderCourse:function(res){
    wx.showToast({
      title: '预约中...',
      icon: 'loading',
      duration: 1000
    });
    var that = this
    var sesskey = that.data.sessionKey
    console.log("ORDER COURSE")
    var publisherid
    var classid
    var nickName
    var avaUrl
    if(res.target.dataset){
      publisherid = res.target.dataset.publisherid
      classid = res.target.dataset.classid
      nickName = that.data.userInfo.nickName
      avaUrl = that.data.userInfo.avatarUrl
      console.log(publisherid + '' + classid)
    }
    if(publisherid != '' && classid != ''){
        var Jsondata = {
          'userOrderClassTeacher':publisherid,
          'userOrderClassId':classid,
          'userNickName':nickName,
          'userAvaUrl':avaUrl
        }
        var data = JSON.stringify(Jsondata)
        var header = { 'content-type': 'application/json', 'Cookie': 'JSESSIONID=' + that.data.sessionKey }
        console.log("发送预约请求")
        util.sendRequest(that.data.orderClassUrl, 'POST', data, header)
          .then(function (reponse) {
            // 成功调用
            if (reponse.data.Status == 'SUCCESS') {
              setTimeout(function () {
                wx.showToast({
                  title: '预约成功',
                  icon: 'success',
                  duration: 2000
                })
              }, 1000)
            }
          }, function (error) {
            setTimeout(function () {
              wx.showToast({
                title: '预约失败',
                icon: 'success',
                duration: 2000
              })
            }, 1000)
            console.log(error)
          })
    }
  }
})